package connectionSettings;


import java.util.Vector;

import javax.microedition.io.Connector;
import javax.microedition.lcdui.Display;

import connections.BTConnectionListener;
import connections.BTSerialConnection;
import connections.Connection;
import connections.ConnectionListener;





public class BTSerialConnectionSettings extends ConnectionSettings implements BTConnectionListener{

	String mode;
	
	boolean discoverServices;

	BTSerialConnection myBTConnection;
	

	public BTSerialConnectionSettings(ConnectionListener _cl ){
		super("BT Terminal",_cl);
		myBTConnection = new BTSerialConnection(this);// "000B53130594"
		
		
		myBTConnection.addListener(_cl);
		myBTConnection.addBlueToothListener(this);
		 myConnection = myBTConnection;
	}

	public BTSerialConnectionSettings(Display _d){
		super("BT Terminal",_d);

		myBTConnection = new BTSerialConnection(this);// "000B53130594"
		myBTConnection.addListener(this);
		myBTConnection.addBlueToothListener(this);
	}
	//public BTSerialConnectionSettings(Display _d,ConnectionListener _cl){
	//	super("BT Terminal", _d);
	//	myBTConnection = new BTSerialConnection(this);// "000B53130594"
	//	myBTConnection.addListener(_cl);
	//	myBTConnection.addBlueToothListener(this);
	//	myConnection = myBTConnection;	
	//}
	

	
/*
	public void  connectionMade(Connection _c, String _address){
		super.connectionMade(_c, _address);
		//transfer the listner to the other guy
		if (cl==null) {
			myBTConnection.addListener(this);
		}else{
			myBTConnection.addListener(cl);
			cl.connectionMade(_c, _address);
		}

	}
	

	public void feedback(Connection _c, String _feedback) {
		if (cl != null) cl.feedback(_c, _feedback);
		super.feedback(_c, _feedback);
	}
	*/
	
	public void displayConnectionInterface(){
		super.displayConnectionInterface();
	
		Vector choices = new Vector();
		choices.addElement("Direct");
		choices.addElement("Discover Services");
		choices.addElement("Discover Quicker");
		choices.addElement("Wait For Connection");
		presentActionChoices("How do you want to connect", choices);
		mode = "Methods";
		//super.displayProtocolOptions();
	}
	
	public void keyDown(int keyCode) {
		myBTConnection.send((byte) keyCode);
	}

	public void setDiscoverServices(boolean _t){
		discoverServices = _t;
	}
	public void madeChoice(int _which, String _choice) {
		System.out.println("Made Choice " + _choice + " in " + mode + " mode.");
		if (mode.equals("Methods")) {
			pickMethod(_choice);

		} else if (mode.equals("Devices")) {

			if(discoverServices){
			myBTConnection.pickDevice(_which); // this also instigates finding services

			}else{
			myBTConnection.connectToSerialServiceOnDevice(_which);
		
			}
		} else if (mode.equals("Services")) {
	
			myBTConnection.pickService(_which);
		
		}
	}
	

	public void feedback(Connection _c, String _s){
		append(_s);
	}

	public void pickMethod(String _which) {
		System.out.println( _which + " Pick Method" + "\n");
		if (_which.equals("Discover Quicker")) {
			System.out.println( "Looking For Devices" + "\n");
			deleteAll();
			setDiscoverServices(false);
			myBTConnection.doDeviceDiscovery();
		} else if (_which.equals("Discover Services")) {
			System.out.println( "Looking For Devices and Services" + "\n");
			deleteAll();
	
			setDiscoverServices(true);
			myBTConnection.doDeviceDiscovery();
		} else if (_which.equals("Wait For Connection")) {
		
			myBTConnection.waitForConnection();
		
		} else if (_which.equals("Direct")) {
			String[] params = getAddressParams();
			String number = params[0];
			if (params.length > 1){
				String name = params[1];
				myBTConnection.setFriendlyName(name);
			}
			//String friendly = directAddress.substring(space+1, directAddress.length());
			if (number.length()>10){
				myBTConnection.connectToHardwareAddress(number);
			}else{
				address.setString("Please specify address for direct connection");
			
			}
		}

	}

	//public boolean madeMenuChoice(String _choice) {
	//	return false;
		
	//}
	public void servicesAvailable(Vector _services) {
		deleteAll();
		presentActionChoices("Pick from " + _services.size() +  " services found:", _services);
		mode = "Services";

	}

	public void devicesAvailable(Vector _devices) {
		deleteAll();
		//String d = (String) _devices.elementAt(0);
		presentActionChoices("Pick from " +_devices.size()+ " devices:" , _devices);
		mode = "Devices";

	}
	
	
	


}
